package com.hhs.storage.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.hhs.storage.bean.*;
import com.hhs.storage.service.BaseDepartmentService;
import com.hhs.storage.service.BaseLogsService;
import com.hhs.storage.service.BaseUserService;
import com.hhs.storage.service.TypeDicService;
import com.hhs.storage.util.StringUtil;
import com.hhs.storage.util.SysUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hhs
 * @since 2022-12-08
 */

@RestController
@RequestMapping("/typeDic")
@ApiSupport(order = 1)
@Api(tags = "相关配置")
public class TypeDicController {
    @Resource
    private BaseLogsService baseLogsService;
    @Resource
    BaseUserService baseUserService;
    @Resource
    BaseDepartmentService baseDepartmentService;
    @Autowired
    private TypeDicService typeDicService;
    //当前方法直接读取了静态数据，用于合同管理——相关配置
    @RequestMapping(value = "table")
    public Map<String, Object> getAll(@RequestParam String keyWord,String typeId) {
//        baseLogsService.log("合同配置","查询",null);

        return typeDicService.getAll(keyWord,typeId);
    }

    //从数据库读取，用于业务管理——相关配置
    @RequestMapping(value = "table2")
    public Map<String, Object> getAll2(TypeDic typeDic,InputParam inputParam) {
        return typeDicService.getAllFromDb2(typeDic,inputParam);
    }

    //从数据库读取，用于业务管理——相关配置
    @RequestMapping(value = "table3")
    @ApiOperation(value = "获取数据",
            notes = "可以通过传值查询，也可以直接获取数据",
            httpMethod = "POST"
    )
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class,name = "page", value = "分页数据",required =
                    false,paramType = "query"),
            @ApiImplicitParam(dataTypeClass = String.class,name = "size", value = "分页数据",required =
                    false,paramType = "query"),
            @ApiImplicitParam(dataTypeClass = String.class,name = "keyWord", value = "类型分类",required =
                    true,paramType = "query"),
            @ApiImplicitParam(dataTypeClass = String.class,name = "id", value = "查询子集时父级的id",required =
                    false,paramType = "query"),
            @ApiImplicitParam(dataTypeClass = String.class,name = "name", value = "搜索名字",required =
                    false,paramType = "query"),
            @ApiImplicitParam(dataTypeClass = String.class,name = "departId", value = "部门id",required =
                    false,paramType = "query"),
            @ApiImplicitParam(dataTypeClass = String.class,name = "updown", value = "查询上下游",required =
                    false,paramType = "query"),
    })
    public Map<String, Object> getAll3(@ApiIgnore TypeDic typeDic,@ApiIgnore InputParam inputParam) {
//        return typeDicService.getAllFromDb2(typeDic,inputParam);
        return typeDicService.query(typeDic,inputParam);

    }

    @RequestMapping(value = "getUpDown")
    @ApiOperation(value = "获取查询上下游",
            httpMethod = "POST")
    @ApiOperationSupport(order = 2)
    public Map<String, Object> getUpDown(@ApiIgnore TypeDic typeDic, @ApiIgnore InputParam inputParam) {
        return typeDicService.getUpDown(typeDic,inputParam);
    }



    @RequestMapping(value = "editUpDown")
    @ApiOperation(value = "更改上下游",
            httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class,name = "upName", value = "上游",required =
                    false,paramType = "query"),
            @ApiImplicitParam(dataTypeClass = String.class,name = "flatName", value = "中游",required =
                    false,paramType = "query"),
            @ApiImplicitParam(dataTypeClass = String.class,name = "downName", value = "下游",required =
                    false,paramType = "query")
    })
    @ApiOperationSupport(order = 1)
    public Map<String, Object> editUpDown(@ApiIgnore TypeDic typeDic, @ApiIgnore InputParam inputParam) {
        return typeDicService.editUpDown(typeDic,inputParam);
    }

    @PostMapping(value = "edit")
    @ApiOperation(value = "更改数据或者新增数据",
            httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class,name = "id", value = "要编辑的数据id",required =
                    false,paramType = "query"),
            @ApiImplicitParam(dataTypeClass = String.class,name = "name", value = "要更改的名称",required =
                    true,paramType = "query"),
            @ApiImplicitParam(dataTypeClass = String.class,name = "departId", value = "部门id",required =
                    false,paramType = "query"),
            @ApiImplicitParam(dataTypeClass = String.class,name = "keyWord", value = "类型分类",required =
                    true,paramType = "query"),
            @ApiImplicitParam(dataTypeClass = String.class,name = "updown", value = "0表示上游，1表示下游",required =
                    false,paramType = "query"),
    })
    @ApiOperationSupport(order = 1)
    public Map<String, Object> saveOrUpdate(TypeDic typeDic){
        //pid是在真空为没有父节点
        if(StringUtil.iszNull(typeDic.getPid())){
            typeDic.setPid(null);
        }
        if (StringUtils.isEmpty(typeDic.getId())){
            baseLogsService.log("合同配置","新增",null);
        }else{
            baseLogsService.log("合同配置","编辑",null);
        }
        return typeDicService.addOrUpdate(typeDic);
    }

    //合同管理——相关配置的删除
    @PostMapping(value = "dele")
    @ApiOperation(value = "删除数据",
            notes = "可以通过传值删除",
            httpMethod = "POST"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class,name = "id", value = "删除的id",required =
                    true,paramType = "query"),
    })
    @ApiOperationSupport(order = 1)
    public Map dele(@ApiIgnore String id) {
        baseLogsService.log("合同配置","删除",null);

        return typeDicService.dele(id);
    }
    //模糊查询
//    @PostMapping(value = "query")
//    public Map<String, Object> query(TypeDic typeDic,InputParam inputParam) {
//
//        return typeDicService.query(typeDic,inputParam);
//    }

    //业务管理——相关配置的删除
    @PostMapping(value = "dele/bussiness")
    public Map delebussiness(String id) {

        return typeDicService.deleForBussiness(id);
    }
    //获取某个关键词的全部数据
    //使用场景： 下单员下单，填写装货地与卸货地
    @GetMapping(value = "allByKey")
    public Map allByKey(String keyWord) {
        return typeDicService.allByKey(keyWord);
    }

    @PostMapping(value = "changeState")
    public Map<String, Object> changeState(TypeDic typeDic){

        if(StringUtils.isEmpty(typeDic.getId()) ||
                typeDic.getState()==null){
            return ResponseInfo.error("数据错误");
        }
        return typeDicService.changeState(typeDic);
    }

    //针对个人计划--业务专属，要求每个公司独立数据
    @PostMapping(value = "getByDepart")
    public Map<String, Object> getByDepart(){
        TypeDic typeDic  = new TypeDic();
        typeDic.setKeyWord(SysUtil.KEY_PLAN_YWTYPE);
        return typeDicService.getByDepart(typeDic);
    }


}

